﻿using System.Linq;
namespace System.Reflection.Extension
{
    public static class FieldInfoExtension
    {
        public static void ForEachAttribute(this FieldInfo fieldInfo, Action<Attribute> callBack)
        {
            object[] attributes = fieldInfo.GetCustomAttributes(true);
            foreach (Attribute attribute in attributes)
                callBack(attribute);
        }

        public static bool HasAttribute<T>(this FieldInfo fieldInfo) where T :Attribute
        {
            bool result = false;
            fieldInfo.ForEachAttribute(a => result = (result || a.GetType().Equals(typeof(T))) );
            return result;
        }

        public static bool isPrimitive(this FieldInfo field)
        {
            return field.FieldType.IsPrimitive || field.FieldType.Equals(typeof(string));
        }

        public static bool isInclusive(this FieldInfo field, params Type[] inclusives)
        {
            return inclusives.Contains(field.FieldType);
        }

        public static bool isClass(this FieldInfo field)
        {
            return field.FieldType.IsClass && field.FieldType.IsAnsiClass && !field.FieldType.IsGenericType &&
                field.FieldType != typeof(string);
        }
        public static bool IsValueType(this FieldInfo field)
        {
            return field.FieldType.IsValueType;
        }
        public static bool isGeneric(this FieldInfo field)
        {
            return field.FieldType.IsGenericType;
        }

        public static bool IsEnum(this FieldInfo field)
        {
            return field.FieldType.IsEnum;
        }
    }
}
